package main

import (
	"fmt"
	"math/rand"
	"sort"
)

/**
  将student的切片按照score从大到小进行排序
*/
type Student struct {
	Name  string
	Age   int
	Score float64
}
type studentSlice []Student

func (s studentSlice) Len() int {
	return len(s)
}

func (s studentSlice) Less(i, j int) bool {
	return s[i].Score > s[j].Score
}

func (s studentSlice) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func main() {
	var newStudents studentSlice
	for i := 0; i < 50; i++ {
		stu := &Student{
			Name:  fmt.Sprintf("学生%d", rand.Intn(500)),
			Age:   int(i/3 + 4),
			Score: float64(rand.Intn(100)),
		}
		newStudents = append(newStudents, *stu)
		sort.Sort(newStudents)
	}
	for _, i2 := range newStudents {
		fmt.Println(i2)
	}
}
